/****************************************************************************************/
/* MediaFrame is an Open Source streaming media platform in Java                        */
/* which provides a fast, easy to implement and extremely small applet                  */
/* that enables to view your audio/video content without having                         */
/* to rely on external player applications or bulky plug-ins.                           */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* "AudioElement.java"                                                                  */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* We changed a lot of code and added a lot of functionality.							*/
/* This includes, but not limited to, the following changes:							*/
/* 1. The project was renamed to MediaFrame;											*/
/* 2. The connection speed detection procedure was added;								*/
/* 3. The JavaScript API functions were added;											*/
/* 4. The pre and post image support functionality was added;							*/
/* 5. The ability to save movie into the local disk was added;							*/
/* 6. The inner buffer for a movie file was added;										*/
/* 7. The click-through functionality was added;    									*/
/* 8. The .zip files support was added;    												*/
/* 9. The realtime feedback agent functionality was added.    							*/
/* For the full list of the current functionality please visit the following web page:	*/
/* http://mediaframe.org/																*/
/*    																					*/
/* 06 Jul 2002 - 19 Dec 2004 Konstantin Belous, Oleg Lebedev							*/
/*																						*/
/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* An AudioElement represents one audio sample. The class is able to parse an audio     */
/* sample from an InputStream and is used by the CutBuffer to buffer audio.             */
/*                                                                                      */
/* -- Ron -- (29-04-2002) r_breukelaar@hotmail.com                                      */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* This program is free software; you can redistribute it and/or modify it under the    */
/* terms of the GNU General Public License as published by the Free Software            */
/* Foundation; either version 2 of the License, or (at your option) any later version.  */
/*                                                                                      */
/* This program is distributed in the hope that it will be useful, but WITHOUT ANY      */
/* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      */
/* PARTICULAR PURPOSE. See the GNU General Public License for more details.             */
/*                                                                                      */
/* You should have received a copy of the GNU General Public License along with this    */
/* program; (See "LICENSE.GPL"). If not, write to the Free Software Foundation, Inc.,   */
/* 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                            */
/*                                                                                      */
/*--------------------------------------------------------------------------------------*/
/*                                                                                      */
/* If the program runs as Java applet it isn't "interactive" in the sense of the GNU    */
/* General Public License. So paragraph 2c doesn't apply.                               */
/*                                                                                      */
/****************************************************************************************/

package mediaframe.mpeg1;

import java.io.*;

/**
 * The <code>AudioFrame</code> class represents one frame of audio data.
 * It extends the <code>DataFrame</code> class and contains methods for reading the audio packet.
 */
public class AudioFrame extends DataFrame
{
	/** The buffer for audio data. */
	private DataBuffer audioBuffer = null;

	/**
	 * Constructs the AudioFrame object with the specified time stamp 
	 * and the buffer for audio data (DataBuffer object).
	 * @param time the time stamp of the frame.
	 * @param audioBuffer the buffer for audio data.
	 */
	public AudioFrame(long time, DataBuffer audioBuffer) {
		super(time, audioBuffer.getBufferPosition());
		this._type = DataFrame.TYPE_AUDIO;
		this.audioBuffer = audioBuffer;
	}

/*
		if (_first_audio_time == -1 && time > -1)
				_first_audio_time = time;
				
	}
	
	private static long _first_audio_time = -1;

	public static long getFirstAudioTime() {
		return _first_audio_time;
	}
*/	
	/**
 	 * This routine reads audio data into the buffer from 
 	 * the input stream to byte which equals 1 
 	 * or until the end of file will be reached.
 	 * @param is the input stream with movie's data.
 	 * @param next32 the last 4 bytes from the input stream.
 	 * @return the last 4 bytes in the stream
 	 */ 
	public long fill(InputStream is, long next32) throws Exception
	{
		long _long = next32;
	
		int r;
		do
		{
			r = is.read();
			if (r != -1)
			{

				audioBuffer.write((char)((_long >>> 24) & 255));

				_long = ((_long << 8) & MASK) | r;
			}
			else
				_eof = true;
		}
		while ((_long >>> 8) != 1 && !_eof);

		if (_eof)
			_long = -1;	

		return _long;
	}

}
